﻿Public Class Form1
  Public Enum attributoTesto
    Normal = 0
    Bold = 1
    Italic = 2
    Underline = 4
  End Enum

  Private selBold As Integer = attributoTesto.Normal
  Private selItalic As Integer = attributoTesto.Normal
  Private selUnderline As Integer = attributoTesto.Normal
  Private selFontFamily As String = ""
  Private selSizeInPoints As Double = 0.0

  Private Sub btnBold_Click(
        ByVal sender As System.Object,
        ByVal e As System.EventArgs) _
        Handles btnBold.Click
    Dim f As New Font(
      RichTextBox1.SelectionFont.FontFamily,
      RichTextBox1.SelectionFont.SizeInPoints,
      RichTextBox1.SelectionFont.Style)
    Me.comboFontFamily.Text =
      f.FontFamily.Name
    Me.comboSizeInPoints.Text =
      f.SizeInPoints.ToString

    If selBold = attributoTesto.Normal Then
      selBold = attributoTesto.Bold
    Else
      selBold = attributoTesto.Normal
    End If

    Dim valoreStile As Integer =
      selBold + selItalic + selUnderline

    Dim fs As New FontStyle
    fs = CType(valoreStile, FontStyle)

    RichTextBox1.SelectionFont =
      New Font(
        RichTextBox1.SelectionFont.FontFamily,
        RichTextBox1.SelectionFont.SizeInPoints, fs)

    Call impostaStatoPulsanti()
  End Sub

  Private Sub btnItalic_Click(
        ByVal sender As System.Object,
        ByVal e As System.EventArgs) _
        Handles btnItalic.Click
    Dim f As New Font(
      RichTextBox1.SelectionFont.FontFamily,
      RichTextBox1.SelectionFont.SizeInPoints,
      RichTextBox1.SelectionFont.Style)
    Me.comboFontFamily.Text =
      f.FontFamily.Name
    Me.comboSizeInPoints.Text =
      f.SizeInPoints.ToString

    If selItalic = attributoTesto.Normal Then
      selItalic = attributoTesto.Italic
    Else
      selItalic = attributoTesto.Normal
    End If

    Dim valoreStile As Integer =
      selBold + selItalic + selUnderline

    Dim fs As New FontStyle
    fs = CType(valoreStile, FontStyle)

    RichTextBox1.SelectionFont =
      New Font(
        RichTextBox1.SelectionFont.FontFamily,
        RichTextBox1.SelectionFont.SizeInPoints, fs)

    Call impostaStatoPulsanti()
  End Sub

  Private Sub btnUnderline_Click(
        ByVal sender As System.Object,
        ByVal e As System.EventArgs) _
        Handles btnUnderline.Click
    Dim f As New Font(
      RichTextBox1.SelectionFont.FontFamily,
      RichTextBox1.SelectionFont.SizeInPoints,
      RichTextBox1.SelectionFont.Style)
    Me.comboFontFamily.Text =
      f.FontFamily.Name
    Me.comboSizeInPoints.Text =
      f.SizeInPoints.ToString

    If selUnderline = attributoTesto.Normal Then
      selUnderline = attributoTesto.Underline
    Else
      selUnderline = attributoTesto.Normal
    End If

    Dim valoreStile As Integer =
      selBold + selItalic + selUnderline

    Dim fs As New FontStyle
    fs = CType(valoreStile, FontStyle)

    RichTextBox1.SelectionFont =
      New Font(
        RichTextBox1.SelectionFont.FontFamily,
        RichTextBox1.SelectionFont.SizeInPoints, fs)

    Call impostaStatoPulsanti()
  End Sub

  Private Sub btnSalva_Click(
        ByVal sender As System.Object,
        ByVal e As System.EventArgs) _
        Handles btnSalva.Click
    Me.RichTextBox1.SaveFile(
      Application.StartupPath & "\file.rtf")
  End Sub

  Private Sub comboFontFamily_SelectedIndexChanged(
        ByVal sender As System.Object,
        ByVal e As System.EventArgs) _
        Handles comboFontFamily.SelectedIndexChanged
    Dim f As New Font(
      comboFontFamily.Text,
      RichTextBox1.SelectionFont.SizeInPoints,
      RichTextBox1.SelectionFont.Style)
    RichTextBox1.SelectionFont = f
  End Sub

  Private Sub comboSizeInPoints_SelectedIndexChanged(
        ByVal sender As System.Object,
        ByVal e As System.EventArgs) _
        Handles comboSizeInPoints.SelectedIndexChanged
    Dim f As New Font(
      RichTextBox1.SelectionFont.FontFamily,
      CType(comboSizeInPoints.Text, Single),
      RichTextBox1.SelectionFont.Style)
    RichTextBox1.SelectionFont = f
  End Sub

  Private Sub RichTextBox1_SelectionChanged(
        ByVal sender As Object,
        ByVal e As System.EventArgs) _
        Handles RichTextBox1.SelectionChanged
    Call impostaStatoPulsanti()
  End Sub

  Public Sub impostaStatoPulsanti()
    Dim f As New Font(
      RichTextBox1.SelectionFont.FontFamily,
      RichTextBox1.SelectionFont.SizeInPoints,
      RichTextBox1.SelectionFont.Style)

    selFontFamily = f.FontFamily.Name
    Me.comboFontFamily.Text = selFontFamily
    selSizeInPoints = f.SizeInPoints
    Me.comboSizeInPoints.Text =
      selSizeInPoints.ToString

    If (f.Style And FontStyle.Bold) =
          attributoTesto.Bold Then
      selBold = attributoTesto.Bold
      Me.btnBold.BackColor = Color.DarkGray
    Else
      selBold = attributoTesto.Normal
      Me.btnBold.BackColor = Color.Transparent
    End If

    If (f.Style And FontStyle.Italic) =
          attributoTesto.Italic Then
      selItalic = attributoTesto.Italic
      Me.btnItalic.BackColor = Color.DarkGray
    Else
      selItalic = attributoTesto.Normal
      Me.btnItalic.BackColor = Color.Transparent
    End If

    If (f.Style And FontStyle.Underline) =
          attributoTesto.Underline Then
      selUnderline = attributoTesto.Underline
      Me.btnUnderline.BackColor = Color.DarkGray
    Else
      selUnderline = attributoTesto.Normal
      Me.btnUnderline.BackColor = Color.Transparent
    End If
  End Sub
End Class
